﻿Imports Global_Visas_Business_Layer
Imports System.Text
Imports System.Data
Imports System.ComponentModel

Partial Public Class frmAddNewCompany
    Public Enum SaveModes
        NotSet = 0
        NewCompany = 1
        AddCompanyToPerson = 2
    End Enum

    Private _SaveMode As SaveModes

    Public Property SaveMode() As SaveModes
        Get
            Return _SaveMode
        End Get
        Set(ByVal value As SaveModes)
            _SaveMode = value
        End Set
    End Property

    Private Sub AddNewCompany_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loaded

        Me.Height = 223
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        If Me.SaveMode = SaveModes.NotSet Then
            CommonFuncs.DisplayMessage("Save Mode Property Not Set", CommonDialog.CustomDialogIcons.Information, "Info")
            Me.Close()
            Exit Sub
        End If

        With Me.drpHeard

            .SelectedValuePath = "wherehear_id"
            .DisplayMemberPath = "wherehear_text"
            .ItemsSource = CType(CommonDataFunctions.WhereHearList, IListSource).GetList()
            .SelectedIndex = -1
        End With

        With Me.drpIndustry
            .SelectedValuePath = "Industry_ID"
            .DisplayMemberPath = "Industry"
            .ItemsSource = CType(Admin.IndustryList, IListSource).GetList()
            .SelectedIndex = -1
        End With

        With Me.drpSize
            .SelectedValuePath = "companysize_id"
            .DisplayMemberPath = "description"
            .ItemsSource = CType(CommonDataFunctions.CompanySizeList, IListSource).GetList()
            .SelectedIndex = -1
        End With

        Me.txtCompanyName.Focus()

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim tsk As New TasksEvents
        Dim cmn As New CommonFuncs
        Dim SQL As StringBuilder
        Dim myData As DataTable
        Dim myCompany As Company.CompanyInfo
        Dim CompanyId As Long
        Dim PersonId As Long
        Dim bFail As Boolean = True

        If Me.txtCompanyName.Text.Trim = String.Empty Then
            CommonFuncs.DisplayMessage("You must provide a name for the company", CommonDialog.CustomDialogIcons.Information, "Company Name")
            Exit Sub
        End If

        If Me.txtCompanyName.Text.Trim.Length > 100 Then
            CommonFuncs.DisplayMessage("Company name cannot be more than 100 characters, please revise", CommonDialog.CustomDialogIcons.Information, "Company Name")
            Exit Sub
        End If

        If Me.drpHeard.SelectedIndex = -1 Then
            If MsgBox("How the person heard about Global Visas could not be added to the database. Do you wish to continue?", MsgBoxStyle.YesNo, "Error") = MsgBoxResult.No Then
                Me.drpHeard.Focus()
                Exit Sub
            End If
            'MsgBox("You must select a where hear type", MsgBoxStyle.Exclamation, "Information")
            'Exit Sub
        End If

        If Me.drpIndustry.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("You must provide a type of company", CommonDialog.CustomDialogIcons.Information, "Company Type")
            Exit Sub
        End If

        If Me.drpSize.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Please supply the size of company", CommonDialog.CustomDialogIcons.Information, "Company Size")
            Exit Sub
        End If

        myData = Company.SimilarCompanies(Me.txtCompanyName.Text)

        If myData.Rows.Count > 0 And Me.lvwSimilar.IsVisible = False Then
            If MsgBox("That company may already exist on the system," & vbCrLf & " would you like to view possible matches?" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "(Selecting no will add a new company record)", vbYesNo, "Company found") = MsgBoxResult.Yes Then
                'show the matches in the list box

                DataBinding.BindListView(myData, Me.lvwSimilar)
                Me.Height = 465
                Me.lvwSimilar.Visibility = Windows.Visibility.Visible
                Me.btnSave.Content = "Create New"
                myData.Dispose()
                Exit Sub
            End If
            myData.Dispose()
        End If

        myCompany = New Company.CompanyInfo

        With myCompany
            .CompanyName = Me.txtCompanyName.Text.Trim
            .CompanySizeId = Me.drpSize.SelectedValue
            .IndustryId = Me.drpIndustry.SelectedValue
        End With
        Try
            CommonFuncs.BeginTrans()

            CompanyId = Company.AddCompany(myCompany, Nothing)

            If CompanyId = 0 Then
                Exit Sub
            End If


            If SaveMode = SaveModes.AddCompanyToPerson Then

            ElseIf SaveMode = SaveModes.NewCompany Then
                'New company, add where hear value

                SQL = New StringBuilder

                With SQL
                    .Append("Insert Into D_Advertising (Advertising_WhereHear, Advertising_DTEntered, Advertising_EnteredBy, Advertising_LinkID , Advertising_LinkType)")
                    .Append(" VALUES('" & cmn.SwapQuotes(Me.drpHeard.Text) & "','" & cmn.FormatDate(Now) & "','" & GlobalCRM.CurrentConName & "'," & CompanyId & ", 1)")
                End With

                If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                    CommonFuncs.RollbackTrans()
                    Exit Sub
                End If

            End If

            'Do goto company stuff

            If PersonId = 0 Then
                Call CommonDataFunctions.GblFunc_Goto_ID(CompanyId, 1)
            Else
                'GblFunc_Add_Event("Account upgraded", "Individual account upgraded to company", 1, PersonId, 2)
                'GblFunc_Goto_ID(PersonId, 2)
            End If

            tsk.GblFunc_Add_SysEvent("Activity", "New company added (" & CompanyId & ")")


            CommonFuncs.CommitTrans()
            bFail = False

            GlobalCRM.CurrentCompanyID = CompanyId
            GlobalCRM.CurrentSearchType = GlobalCRM.enuSearchType.eCompany
            'frmMainDisplay.MdiParent = frmMDIMain
            'frmMainDisplay.Show()
            'show maindisplay - Sumathivinayak S
            Me.Close()

        Catch ex As Exception
            CommonFuncs.DisplayMessage("There was an error saving new company." & vbCrLf & "Please retry then ask support if no success.", CommonDialog.CustomDialogIcons.Information, "Assign Case Worker")
            If CommonFuncs.IsTransactionOpen = True Then
                CommonFuncs.RollbackTrans()
            End If
            CommonFuncs.ErrorHandler(ex, "frmAddNewCompany.btnSave_Click", True)
        Finally
            If bFail = True Then
                CommonFuncs.DisplayMessage("There was an error saving new company." & vbCrLf & "Please retry then ask support if no success.", CommonDialog.CustomDialogIcons.Information, "Assign Case Worker")
            End If
            cmn = Nothing
            SQL = Nothing

            tsk = Nothing
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub


    Private Sub lvwSimilar_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)

        GlobalCRM.CurrentCompanyID = Me.lvwSimilar.SelectedItems(0).row.itemarray(0).ToString
        CommonDataFunctions.GblFunc_Goto_ID(GlobalCRM.CurrentCaseID, CommonDataFunctions.DBIdTypes.CompanyID)
        'frmMainDisplay.MdiParent = frmMDIMain
        'frmMainDisplay.Show()
        'show maindisplay - Sumathivinayak S
        Me.Close()

    End Sub
    
End Class
